﻿using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

namespace Selenium.PageToolkit
{
    public static class PageObjectExt
    {
        private static LambdaPageFactory _sharedFactory;

        public static LambdaPageFactory UseForInject(this LambdaPageFactory f)
        {
            _sharedFactory = f;
            return f;
        }

        public static T VerifyLoaded<T>(this T p) where T : IPageObject
        {
            var driver = (IWebDriver)_sharedFactory.DriverFactory();
            new WebDriverWait(driver, TimeSpan.FromSeconds(15)).Until(q => p.UrlMatch(q.Url));

            return p;
        }

        public static T Inject<T>(this T p) where T : IPageObject
        {
            _sharedFactory.Inject(p);
            return p;
        }

        public static IWebElement Type(this IWebElement e, string text)
        {
            e.Clear();
            e.SendKeys(text);
            return e;
        }

    }
}